# archive_debian_12_deb_packages.py
# Create by GF 2025-05-19 20:45

# Example:
# >>> import archive_debian_12_deb_packages
# >>>
# >>> Packages = archive_debian_12_deb_packages.Packages()
# >>>
# >>> Packages.path.update("./Packages.gz")  # 1. Locate the position of "Packages.gz".
# >>> Packages.read()                        # 2. Read "Packages.gz" content as a text string.
# >>> Packages.find_Filename()               # 3. Find all "Filename" in the "Packages.gz" content.
# >>> print(Packages.content.Filename)
# ['Filename: pool/contrib/1/1oom/1oom_1.0-2_amd64.deb',
#  'Filename: pool/contrib/a/alien-arena/alien-arena_7.71.3+dfsg-3_amd64.deb',
#  'Filename: pool/contrib/a/alien-arena/alien-arena-server_7.71.3+dfsg-3_amd64.deb',
#  ......
#  'Filename: pool/contrib/z/zmat/matlab-zmat_0.9.8+ds-8_all.deb',
#  'Filename: pool/contrib/z/zsnapd/zsnapd_0.8.12-1_all.deb',
#  'Filename: pool/contrib/z/zsnapd/zsnapd-rcmd_0.8.12-1_all.deb']

import gzip
import re

# ##################################################

class path:
    
    def __init__(self):

        self.Packages = "./Packages.gz"

    def update(self, path_str:str):

        """ 更新字符串到 path 中 """

        self.Packages = path_str
        # ..........................................
        return self  # 返回 self 以实现链式调用

# ##################################################

class content:
    
    def __init__(self):

        self.text     = ''
        self.Filename = []

    def update_text(self, text_str:str):

        """ 更新列表串到 text 中 """

        self.text = text_str
        # ..........................................
        return self  # 返回 self 以实现链式调用

    def update_Filename(self, Filename_List:list):

        """ 更新列表串到 Filename 中 """

        self.Filename = Filename_List
        # ..........................................
        return self  # 返回 self 以实现链式调用

# ##################################################

class Packages:
    
    def __init__(self):

        self.path    = path()
        self.content = content()

    def read(self):
        
        gzip_file = gzip.open(self.path.Packages, mode="rb")
        byts_ctnt = gzip_file.read()
        gzip_file.close()
        text_ctnt = byts_ctnt.decode("utf-8")
        self.content.update_text(text_ctnt)
        # ..........................................
        return self  # 返回 self 以实现链式调用
    
    def find_Filename(self):

        # ......
        # Section: contrib/games
        # Priority: optional
        # Filename: pool/contrib/1/1oom/1oom_1.0-2_amd64.deb    <- Match Target
        # Size: 506180
        # MD5sum: d6ddb9189f14e76d7336246104cd0d2c
        # ......

        match_text = re.findall("Filename.*", self.content.text)
        self.content.update_Filename(match_text)
        # ..........................................
        return self  # 返回 self 以实现链式调用

# Signed by GF.
